/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.controller;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import edu.syr.iis.web.model.DialogueAct;
import edu.syr.iis.web.model.DialogueActDimensionManager;
import edu.syr.iis.web.model.DialogueActLabel;
import edu.syr.iis.web.model.DialogueActLabelManager;
import edu.syr.iis.web.model.DialogueActManager;
import edu.syr.iis.web.model.DialogueActSchemeManager;
import edu.syr.iis.web.model.Message;
import edu.syr.iis.web.model.MessageManager;
import edu.syr.iis.web.util.Key;

/**
 * @author kinoue
 *
 */
public class MergeDialogueActLabelController extends SimpleFormController
{
	private DialogueActLabelManager dialogueActLabelManager = null;
	private DialogueActDimensionManager dialogueActDimensionManager = null;
	private DialogueActSchemeManager dialogueActSchemeManager = null;
	
	private MessageManager messageManager = null;
	private DialogueActManager dialogueActManager = null;

	private DialogueActLabel dalToBeDeleted = null;
	private DialogueActLabel dalToBeMerged = null;

	/**
	 * Returns a new instance of DialogueActLabel object if "dialogueActLabelId" 
	 * HTTP parameter is specified; other returns instance of object in database 
	 * matching the value of the parameter.
	 * @see DialogueActLabel
	 */

	@Override
	protected Object formBackingObject(HttpServletRequest request)
	{
		if (request.getParameter(Key.DA_LABEL_ID) != null
				&& request.getParameter(Key.DA_LABEL_ID).trim().length() > 0) 
		{
			int id = Integer.parseInt(request.getParameter(Key.DA_LABEL_ID));
			dalToBeDeleted = dialogueActLabelManager.getDialogueActLabel(id);
		} 
		dalToBeMerged = new DialogueActLabel();
		
		return dalToBeMerged;
	}   

	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception
	{
		HashMap model = new HashMap();
		model.put(Key.DA_LABELS, dialogueActLabelManager.getDialogueActLabels());
		model.put(Key.DA_DIMENSIONS, dialogueActDimensionManager.getDialogueActDimensions());
		model.put(Key.DA_SCHEMES, dialogueActSchemeManager.getDialogueActSchemes());				

		return model;
	}

	@Override
	protected ModelAndView onSubmit(
			HttpServletRequest request,
			HttpServletResponse response,
			Object command,
			BindException errors)
	{
		DialogueActLabel dal = (DialogueActLabel) command;
		dalToBeMerged = dialogueActLabelManager.getDialogueActLabel(dal.getDialogueActLabelId());
		
		System.out.println("Merging " + dalToBeDeleted.getLabel() + "(" + dalToBeDeleted.getDialogueActLabelId() + ")");
		System.out.println(" to "  + dalToBeMerged.getLabel() + "(" + dalToBeMerged.getDialogueActLabelId() + ")");

		mergeDialogueActLabel(dalToBeDeleted, dalToBeMerged);
		return new ModelAndView(getSuccessView());
	}

	
	void mergeDialogueActLabel(DialogueActLabel dal, DialogueActLabel dalMergedTo) {
		
		Collection<Message> messages = dal.getMessages();
		Collection<DialogueAct> functionDas = dal.getFunctionDialogueActs();
		Collection<DialogueAct> domainDas = dal.getDomainDialogueActs();
		
		for (Message message: messages) 
		{
			message.getDialogueActLabels().remove(dal);
			message.getDialogueActLabels().add(dalMergedTo);			
			messageManager.saveMessage(message);
		}
		
		for (DialogueAct da: functionDas) 
		{
			da.setFunction(dalMergedTo);
			dialogueActManager.saveDialogueAct(da);
		}
		
		for (DialogueAct da: domainDas) 
		{
			da.setDomain(dalMergedTo);
			dialogueActManager.saveDialogueAct(da);
		}

		dialogueActLabelManager.deleteDialogueActLabel(dal.getDialogueActLabelId());			
	}
	
	public void setDialogueActLabelManager(
			DialogueActLabelManager dialogueActLabelManager)
	{
		this.dialogueActLabelManager = dialogueActLabelManager;
	}
	
	public void setDialogueActDimensionManager(
			DialogueActDimensionManager dialogueActDimensionManager)
	{
		this.dialogueActDimensionManager = dialogueActDimensionManager;
	}

	public void setDialogueActSchemeManager(
			DialogueActSchemeManager dialogueActSchemeManager)
	{
		this.dialogueActSchemeManager = dialogueActSchemeManager;
	}

	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}

	public void setDialogueActManager(DialogueActManager dialogueActManager) {
		this.dialogueActManager = dialogueActManager;
	}
	
	
}
